<?php

class Article extends Zend_Db_Table
{
    protected $_name = 'article';
    
    public function getInfo($id)
    {
        $select = $this->getAdapter();
        $prepare = $select->query('SELECT article.*
                                   FROM article
                                   WHERE article.id='.$id);
        $result = $prepare->fetchAll(Zend_Db::FETCH_ASSOC);
        return $result[0];
    }    
    
    public function getPublished()
    {
        $select = $this->getAdapter();
        $prepare = $select->query('SELECT article.*
                                   FROM article
                                   WHERE article.published=1');
        $result = $prepare->fetchAll(Zend_Db::FETCH_ASSOC);
        return $result;
    }    
    
    public function getCategories()
    {
        $return = array(0=>'');
        $select = $this->getAdapter();
        $prepare = $select->query('SELECT category.*
                                   FROM category
                                   WHERE category.published=1');
        $result = $prepare->fetchAll(Zend_Db::FETCH_ASSOC);
        foreach($result as $row){
            $return[$row['id']] = $row['name'];
        }
        return $return;
    }
    
    public function getMaxId()
    {
        $select = $this->getAdapter();
        $prepare = $select->query('SELECT max(id) as max_id
                                   FROM article');
        $result = $prepare->fetchAll(Zend_Db::FETCH_ASSOC);
        return $result[0]['max_id'];
    }   
    
    public function getArticlesByCategory($alias)
    {
        $select = $this->getAdapter();
        $prepare = $select->query('SELECT article.*, category.name 
                                   FROM article
                                   INNER JOIN category on article.category_id = category.id
                                   WHERE category.alias="'.$alias.'"
                                   AND article.published=1
                                   ORDER BY article.id DESC');
        $result = $prepare->fetchAll(Zend_Db::FETCH_ASSOC);
        return $result;
    } 
    
     
}
